/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.service.content;


import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.entity.MqJms;
import han.bifang.gwt.client.common.util.StringUtils;
import han.bifang.gwt.client.service.ServiceMessages;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;

/**
 * 
 * @Author Kent
 */
public class MqJmsPanel extends Content {

	private TextBox host, qmManager, qmChannel,qmPort, queue, ccsid;
	
	private ServiceMessages cons;
	
	private MqJms info ;
	
	public MqJmsPanel(ServiceMessages scons, MqJms info1) {
		this.cons = scons;
		this.info = info1;
		
		
		VerticalPanel panel = new VerticalPanel();
		
		panel.setWidth("100%");
		
		Grid jms = new Grid(6, 2);
		jms.setWidth("80%");
		jms.setCellPadding(1);
		jms.setCellSpacing(1);
		jms.setBorderWidth(1);

		ColumnFormatter cf = jms.getColumnFormatter();
		cf.setWidth(0, "15%");
		cf.setWidth(1, "55%");

		CellFormatter ff = jms.getCellFormatter();
		ff.setStyleName(0, 0, "tablecol");
		ff.setStyleName(1, 0, "tablecol");
		ff.setStyleName(2, 0, "tablecol");
		ff.setStyleName(3, 0, "tablecol");
		ff.setStyleName(4, 0, "tablecol");
		ff.setStyleName(5, 0, "tablecol");
		
		jms.setHTML(0, 0, cons.host());
		jms.setHTML(1, 0, cons.qmMananger());
		jms.setHTML(2, 0, cons.qmPort());
		jms.setHTML(3, 0, cons.qmChannel());
		jms.setHTML(4, 0, cons.queue());
		jms.setHTML(5, 0, cons.ccsid());
		
		
		
		host = new TextBox();
		host.setVisibleLength(64);

		qmManager = new TextBox();
		qmManager.setVisibleLength(40);

		qmPort = new TextBox();
		qmPort.setVisibleLength(16);
		
		qmChannel = new TextBox();
		qmChannel.setVisibleLength(40);
		
		queue = new TextBox();
		queue.setVisibleLength(40);
		
		ccsid = new TextBox();
		ccsid.setVisibleLength(16);
		
		jms.setWidget(0, 1, host);
		jms.setWidget(1, 1, qmManager);
		jms.setWidget(2, 1, qmPort);
		jms.setWidget(3, 1, qmChannel);
		jms.setWidget(4, 1, queue);
		jms.setWidget(5, 1, ccsid);
		
		
		if(info != null){
			host.setText(info.getMqHost());
			qmManager.setText(info.getQmManager());
			qmPort.setText(info.getQmPort());
			qmChannel.setText(info.getQmChannel());
			queue.setText(info.getQueue());
			ccsid.setText(info.getCcsid());
		}
		
		panel.add(jms);
		
		initWidget(panel);

	}
	
	public MqJms getMqJms(){
		if (!StringUtils.checkLenght(host.getText(), 1, 512)) {
			Window.alert(cons.lengthError(cons.host(), "1", "512"));
			return null;
		}
		if (!StringUtils.checkLenght(qmManager.getText(), 1, 128)) {
			Window.alert(cons.lengthError(cons.qmMananger(), "1", "128"));
			return null;
		}
		if (!StringUtils.checkLenght(qmPort.getText(), 1, 8)) {
			Window.alert(cons.lengthError(cons.qmPort(), "1", "8"));
			return null;
		}
		if (!StringUtils.checkLenght(qmChannel.getText(), 1, 256)) {
			Window.alert(cons.lengthError(cons.qmChannel(), "1", "256"));
			return null;
		}
		if (!StringUtils.checkLenght(queue.getText(), 1, 256)) {
			Window.alert(cons.lengthError(cons.queue(), "1", "256"));
			return null;
		}
		if (!StringUtils.checkLenght(ccsid.getText(), 1, 20)) {
			Window.alert(cons.lengthError(cons.ccsid(), "1", "20"));
			return null;
		}
		if(info == null)
			info = new MqJms();
		info.setCcsid(ccsid.getText());
		info.setMqHost(host.getText());
		info.setQmChannel(qmChannel.getText());
		info.setQmManager(qmManager.getText());
		info.setQmPort(qmPort.getText());
		info.setQueue(queue.getText());
		return info;
	}

}
